﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WuxiWTO.Mapping;
using WutiWTO.Common;
using System.Data.Linq;


namespace WuxiWTO.Web.backend
{
    public partial class userDetails : BasePage
    {
        WTODataContext contex = new WTODataContext();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int uid = int.Parse(Request.QueryString["id"]);
                GetUserInfo(uid);
            }
            this.btnDelete1.Attributes.Add("onclick", "return confirm('您确定要删除该用户吗?');");
            this.btnDelete2.Attributes.Add("onclick", "return confirm('您确定要删除该用户吗?');");
            this.btnUnPass1.Attributes.Add("onclick", "return openValidate(" + Request.QueryString["id"] + ");");
            this.btnUnPass2.Attributes.Add("onclick", "return openValidate(" + Request.QueryString["id"] + ");");
        }

        private void GetUserInfo(int uid)
        {
            supporter user = contex.supporters.SingleOrDefault(p => p.supporter_id == uid);
            if (user != null)
            {
                if (user.supporter_type == 'I')
                {
                    panIndividual.Visible = true;
                    panCompany.Visible = false;
                    this.lblEmailI.Text = user.email;
                    this.lblIdentity.Text = user.identity_card;
                    this.lblLoginNameI.Text = user.supporter_login_name;
                    this.lblNameI.Text = user.supporter_name;
                    this.lblPasswordI.Text = user.supporter_password;
                    //this.lblPurposeI.Text = GetPurposeByCode(user.purpose);
                    this.lblTelephoneI.Text = user.telephone;
                    this.litValidateMsg1.Text = string.IsNullOrEmpty(user.validate_reason) ? "" : "(" + user.validate_reason + ")";
                    if (user.is_active == 'Y')
                    {
                        this.lblValidateI.Text = "已通过审核";
                        this.lblValidateI.ForeColor = System.Drawing.Color.Green;
                        this.btnValidate1.Visible = false;
                        this.btnUnPass1.Visible = false;
                    }
                    else if (user.is_active == 'N')
                    {
                        this.lblValidateI.Text = "未审核";
                        this.lblValidateI.ForeColor = System.Drawing.Color.Red;
                        this.btnValidate1.Visible = true;
                        this.btnUnPass1.Visible = true;
                    }
                    else
                    {
                        this.lblValidateI.Text = "未通过审核";
                        this.lblValidateI.ForeColor = System.Drawing.Color.Red;
                        this.btnValidate1.Visible = true;
                        this.btnUnPass1.Visible = false;

                    }
                }
                else
                {
                    panIndividual.Visible = false;
                    panCompany.Visible = true;
                    this.lblEmailO.Text = user.email;
                    this.lblEnterpriseName.Text = user.enterprise_name;
                    this.lblLoginNameO.Text = user.supporter_login_name;
                    this.lblNameO.Text = user.enterprise_name;
                    //this.lblPurposeO.Text = GetPurposeByCode(user.purpose);
                    this.lblTelephoneO.Text = user.mobile;
                    this.lblContact.Text = user.contact;
                    this.lblPosition.Text = user.position;
                    this.lblAddress.Text = user.enterprise_address;
                    this.litValidateMsg1.Text = string.IsNullOrEmpty(user.validate_reason) ? "" : "(" + user.validate_reason + ")";
                    if (user.is_active == 'Y')
                    {
                        this.lblValidateO.Text = "已通过审核";
                        this.lblValidateO.ForeColor = System.Drawing.Color.Green;
                        this.btnValidate2.Visible = false;
                        this.btnUnPass2.Visible = false;
                        
                    }
                    else if (user.is_active == 'N')
                    {
                        this.lblValidateO.Text = "未审核";
                        this.lblValidateO.ForeColor = System.Drawing.Color.Red;
                        this.btnValidate2.Visible = true;
                        this.btnUnPass2.Visible = true;
                    }
                    else
                    {
                        this.lblValidateO.Text = "未通过审核";
                        this.lblValidateO.ForeColor = System.Drawing.Color.Red;
                        this.btnValidate2.Visible = true;
                        this.btnUnPass2.Visible = false;

                    }
                }
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "fa", "alert('参数传递失败');", true);
                return;
            }
        }

        private string GetPurposeByCode(string code)
        {
            string purpose = "";
            switch (code)
            {
                case "2": purpose = "外商投诉"; break;
                case "3": purpose = "培训报名"; break;
                case "4": purpose = "其他原因"; break;
                default: break;
            }
            return purpose;
        }

        protected void btnValidate_Click(object sender,EventArgs e)
        {
            int supporter_id = Convert.ToInt32(Request.QueryString["id"]);
            supporter cat = contex.supporters.SingleOrDefault(p => p.supporter_id == supporter_id);
            if (cat != null)
            {
                cat.is_active = 'Y';
                cat.validate_reason = "";
                contex.SubmitChanges();
                Response.Redirect("userList.aspx");
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "fa2", "alert('参数传递失败');", true);
            }
        }
        protected void btnUnPass_Click(object sender, EventArgs e)
        {
            int supporter_id = Convert.ToInt32(Request.QueryString["id"]);
            supporter cat = contex.supporters.SingleOrDefault(p => p.supporter_id == supporter_id);
            if (cat != null)
            {
                cat.is_active = 'F';
                contex.SubmitChanges();
                Response.Redirect("userList.aspx");
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "fa2", "alert('参数传递失败');", true);
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            int supporter_id = Convert.ToInt32(Request.QueryString["id"]);
            supporter cat = contex.supporters.SingleOrDefault(p => p.supporter_id == supporter_id);
            if (cat != null)
            {
                contex.supporters.DeleteOnSubmit(cat);
                contex.SubmitChanges();
                Response.Redirect("userList.aspx");
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "fa1", "alert('参数传递失败');", true);
            }
        }
    }
}